/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import model.Contact;
import model.ContactGroup;
import model.ContactGroupManager;
import model.ContactNotFoundException;
import controller.ContactManager;

/**
 *
 * @author hbriand
 * 
 * @class ContactGroupForm
 * @desc The form used to edit or create a new group of contacts
 */
public class ContactGroupForm implements WindowListener{

    private ContactGroup currentContact;
    private JFrame frame;
    private JTextField nameTxt;
    private JList inList;
    private DefaultListModel inListModel;
    private JList outList;
    private DefaultListModel outListModel;

    private ContactGroupFormDelegate delegate;

    public ContactGroupForm(ContactGroup edit) {
        this.currentContact = edit;
        this.frame = new JFrame();
        this.draw();
    }

    public ContactGroupForm() {
        this.currentContact = new ContactGroup("");
        this.frame = new JFrame();
        this.draw();
    }

    public void show() {
        this.frame.setVisible(true);
    }

    public void hide() {
        this.frame.setVisible(false);
    }


    public ContactGroup getCurrentContact() {
        return currentContact;
    }

    public void setCurrentContact(ContactGroup currentContact) {
        this.currentContact = currentContact;
    }

    private void draw() {
        this.frame.setSize(800, 600);
        this.frame.setLayout(new BorderLayout());
        this.frame.add(this.getNameComponent(), BorderLayout.PAGE_START);
        this.frame.add(this.getContactListComponent(), BorderLayout.LINE_START);
        this.frame.add(this.getContactOutListComponent(), BorderLayout.LINE_END);
        this.frame.add(this.getActionButtons(), BorderLayout.PAGE_END);
    }

    private JPanel getNameComponent() {
        JPanel nComp = new JPanel();
        nComp.setLayout(new FlowLayout());
        nComp.add(new JLabel("Group name :"));
        this.nameTxt = new JTextField(this.currentContact.getName(), 50);
        nComp.add(nameTxt);
        return nComp;
    }

    private JPanel getContactListComponent() {
        JPanel listComp = new JPanel();
        listComp.setLayout(new BoxLayout(listComp, BoxLayout.PAGE_AXIS));
        listComp.add(new JLabel("Current contacts in group :"));
        this.inList = new JList();
        this.inListModel = new DefaultListModel();
        for(Contact c : this.currentContact.getSortedContactList()) {
            this.inListModel.addElement(c.fullName());
        }
        this.inList.setModel(this.inListModel);
        listComp.add(this.inList);
        JButton remBtn = new JButton("Remove from group");
        remBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String s = (String)inListModel.remove(inList.getSelectedIndex());
                outListModel.addElement(s);
            }
        });
        listComp.add(remBtn);
        return listComp;
    }

    private Component getContactOutListComponent() {
        JPanel listComp = new JPanel();
        listComp.setLayout(new BoxLayout(listComp, BoxLayout.PAGE_AXIS));
        listComp.add(new JLabel("Current contacts in group :"));
        this.outList = new JList();
        this.outListModel = new DefaultListModel();
        for(Contact c : ContactGroupManager.getInstance().getAllSortedContactsNotInGroup(this.currentContact)) {
            this.outListModel.addElement(c.fullName());
        }
        this.outList.setModel(this.outListModel);
        listComp.add(this.outList);
        JButton remBtn = new JButton("Add to group");
        remBtn.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String s = (String)outListModel.remove(outList.getSelectedIndex());
                inListModel.addElement(s);
            }
        });
        listComp.add(remBtn);
        return listComp;
    }

    private Component getActionButtons() {
        JPanel actionButtons = new JPanel();
        actionButtons.setLayout(new FlowLayout());

        JButton add = new JButton("Save");
        add.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                currentContact.setName(nameTxt.getText());
                for (int i = 0; i<inListModel.getSize(); i++) {
                    try {
                        ContactGroupManager.getInstance().addFullNamedContactToGroup((String) inListModel.get(i), currentContact);
                    } catch (ContactNotFoundException ex) {
                        Logger.getLogger(ContactGroupForm.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                hide();
                delegate.contactGroupFormAdd(currentContact);
            }

        });
        actionButtons.add(add);

        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                currentContact.removeFromCGList();
                hide();
                delegate.contactGroupFormCancel();
            }
        });
        actionButtons.add(cancel);

        return actionButtons;
    }

    public void setDelegate(ContactManager aThis) {
        this.delegate = aThis;
    }

    public void windowOpened(WindowEvent e) {
    }

    public void windowClosing(WindowEvent e) {
        this.delegate.contactGroupFormCancel();
    }

    public void windowClosed(WindowEvent e) {
    }

    public void windowIconified(WindowEvent e) {
    }

    public void windowDeiconified(WindowEvent e) {
    }

    public void windowActivated(WindowEvent e) {
    }

    public void windowDeactivated(WindowEvent e) {
    }

}
